package com.ruoyi.gouhuoruku.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.cangku.domain.Cangku;
import com.ruoyi.cangku.domain.Kucunpandian;
import com.ruoyi.cangku.mapper.CangkuMapper;
import com.ruoyi.cangku.mapper.KucunpandianMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.goods.domain.Shangpinxinxi;
import com.ruoyi.goods.mapper.ShangpinxinxiMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gouhuoruku.mapper.GouhuoRukuMapper;
import com.ruoyi.gouhuoruku.domain.GouhuoRuku;
import com.ruoyi.gouhuoruku.service.IGouhuoRukuService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 购货入库Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-22
 */
@Service
public class GouhuoRukuServiceImpl implements IGouhuoRukuService
{
    @Autowired
    private GouhuoRukuMapper gouhuoRukuMapper;

    @Autowired
    private CangkuMapper cangkuMapper;

    @Autowired
    private ShangpinxinxiMapper shangpinxinxiMapper;

    @Autowired
    private KucunpandianMapper kucunpandianMapper;

    /**
     * 查询购货入库
     *
     * @param id 购货入库主键
     * @return 购货入库
     */
    @Override
    public GouhuoRuku selectGouhuoRukuById(Long id)
    {
        return gouhuoRukuMapper.selectGouhuoRukuById(id);
    }

    /**
     * 查询购货入库列表
     *
     * @param gouhuoRuku 购货入库
     * @return 购货入库
     */
    @Override
    public List<GouhuoRuku> selectGouhuoRukuList(GouhuoRuku gouhuoRuku)
    {
        return gouhuoRukuMapper.selectGouhuoRukuList(gouhuoRuku);
    }

    /**
     * 新增购货入库
     *
     * @param gouhuoRuku 购货入库
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertGouhuoRuku(GouhuoRuku gouhuoRuku)
    {
        Cangku cangku = cangkuMapper.selectCangkuById(gouhuoRuku.getGouhuoCangkuId());
        Shangpinxinxi shangpin = shangpinxinxiMapper.selectShangpinxinxiById(gouhuoRuku.getGoodId());
        gouhuoRuku.setGouhuoDanhao(OrderUtils.createOrderSN());
        gouhuoRuku.setGouhuoCangku(cangku.getCangkuName());
        gouhuoRuku.setGoodName(shangpin.getShangpinName());
        gouhuoRuku.setGoodGuige(shangpin.getShangpinGuige());
        gouhuoRuku.setJine(gouhuoRuku.getShuliang().multiply(shangpin.getShangpinPifajia()));
        gouhuoRuku.setGoodBianma(shangpin.getShangpinId());
        gouhuoRukuMapper.insertGouhuoRuku(gouhuoRuku);

        Kucunpandian fitter = new Kucunpandian();
        fitter.setCangkuId(gouhuoRuku.getGouhuoCangkuId());
        fitter.setGoodId(gouhuoRuku.getGoodId());
        Kucunpandian nowKucunpandian = kucunpandianMapper.selectKucunpandianByGoodIdAndCangkuId(fitter);

        if(nowKucunpandian == null){
            Kucunpandian newPandian = new Kucunpandian();
            newPandian.setCangkuName(cangku.getCangkuName());
            newPandian.setCangkuId(cangku.getId());
            newPandian.setGoodId(shangpin.getId());
            newPandian.setGoodName(shangpin.getShangpinName());
            newPandian.setGoodSpecification(shangpin.getShangpinGuige());
            newPandian.setKuncun(gouhuoRuku.getShuliang());
            newPandian.setGoodBianma(shangpin.getShangpinId());
            return kucunpandianMapper.insertKucunpandian(newPandian);
        }else{
            BigDecimal count = nowKucunpandian.getKuncun();
            BigDecimal nowCount = count.add(gouhuoRuku.getShuliang());
            nowKucunpandian.setKuncun(nowCount);
            return kucunpandianMapper.updateKucunpandian(nowKucunpandian);
        }

    }

    /**
     * 修改购货入库
     *
     * @param gouhuoRuku 购货入库
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateGouhuoRuku(GouhuoRuku gouhuoRuku)
    {
        GouhuoRuku oldGouhuoruku = gouhuoRukuMapper.selectGouhuoRukuById(gouhuoRuku.getId());
        Kucunpandian fitter = new Kucunpandian();
        fitter.setGoodId(gouhuoRuku.getGoodId());
        fitter.setCangkuId(gouhuoRuku.getGouhuoCangkuId());
        Kucunpandian oldKucunpandian = kucunpandianMapper.selectKucunpandianByGoodIdAndCangkuId(fitter);
        BigDecimal nowCount = oldKucunpandian.getKuncun().add(gouhuoRuku.getShuliang().subtract(oldGouhuoruku.getShuliang()));

        if(nowCount.compareTo(BigDecimal.ZERO) >= 0) {
            oldKucunpandian.setKuncun(nowCount);
            kucunpandianMapper.updateKucunpandian(oldKucunpandian);
            gouhuoRukuMapper.updateGouhuoRuku(gouhuoRuku);
            return AjaxResult.success("修改购货入库成功");
        }else{
            return AjaxResult.error("修改购货入库失败,商品库存"+oldKucunpandian.getKuncun());
        }
    }

    /**
     * 批量删除购货入库
     *
     * @param ids 需要删除的购货入库主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteGouhuoRukuByIds(Long[] ids)
    {
        for(Long item:ids){
            GouhuoRuku oldGouhuoruku = gouhuoRukuMapper.selectGouhuoRukuById(item);
            Kucunpandian fitter = new Kucunpandian();
            fitter.setGoodId(oldGouhuoruku.getGoodId());
            fitter.setCangkuId(oldGouhuoruku.getGouhuoCangkuId());
            Kucunpandian oldKucunpandian = kucunpandianMapper.selectKucunpandianByGoodIdAndCangkuId(fitter);
            BigDecimal nowCount = oldKucunpandian.getKuncun().subtract(oldGouhuoruku.getShuliang());
            if(nowCount.compareTo(BigDecimal.ZERO) >= 0) {
                oldKucunpandian.setKuncun(nowCount);
                kucunpandianMapper.updateKucunpandian(oldKucunpandian);
            }else{
                throw new RuntimeException("选中购货入库记录 "+oldGouhuoruku.getGoodName()+" 商品库存不足,不能删除");
            }
        }
        return gouhuoRukuMapper.deleteGouhuoRukuByIds(ids);
    }

    /**
     * 删除购货入库信息
     *
     * @param id 购货入库主键
     * @return 结果
     */
    @Override
    public int deleteGouhuoRukuById(Long id)
    {
        return gouhuoRukuMapper.deleteGouhuoRukuById(id);
    }
}
